package com.yjd.pub.fundservice.service;

import com.alibaba.fastjson.JSON;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.enums.OrderRefuseEnum;
import com.yjd.comm.fundservice.dto.QKLoanNoticeModel;
import com.yjd.comm.fundservice.enums.BusTypeEnum;
import com.yjd.comm.fundservice.enums.LoanApplyStatusEnum;
import com.yjd.comm.fundservice.model.FundLoanApplyModel;
import com.yjd.comm.fundservice.service.ICallbackService;
import com.yjd.comm.fundservice.service.IFundLoanApplyService;
import com.yjd.comm.msg.MnsMsgInfo;
import com.yjd.comm.order.model.OrderModel;
import com.yjd.comm.order.service.IOrderService;
import com.yjd.comm.util.FrameUtil;
import com.yjd.comm.util.StringUtil;
import com.yjd.msg.comm.annotation.MnsDispose;
import com.yjd.msg.comm.annotation.MnsMsg;
import com.yjd.msg.comm.enums.MsgOrderServiceEnum;
import com.yjd.msg.comm.enums.MsgTypeEnum;
import com.yjd.msg.comm.util.MnsUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;

/**
 * 回调处理（如贷款回调）
 * Created by yangchangyan on 2018/1/29.
 */
@Service("callbackService")
public class CallbackServiceImpl implements ICallbackService {

    protected Logger logger = Logger.getLogger(getClass());

    @Resource
    private IOrderService orderService;

    @Resource
    private IFundLoanApplyService fundLoanApplyService;

    @MnsMsg
    @Override
    public void callbackProcess(QKLoanNoticeModel notice) throws Exception {
        Assert.notNull(notice, "回调消息不正确");

        // 发送借款回调消息
        MnsUtils.createMessage(notice.getAgent_order_id(), MsgTypeEnum.LOAN_APPLY_REPLY_MSG, MsgOrderServiceEnum.PUBLIC_ORDER_SERVICE, notice);

    }

    @MnsDispose
    @Override
    public void loanApplyReplyMsgDispose(MnsMsgInfo mnsMsgInfo) throws Exception {
        logger.info("贷款消息："+ JSON.toJSONString(mnsMsgInfo));
        QKLoanNoticeModel notice = StringUtil.getObject(mnsMsgInfo.getBody(), QKLoanNoticeModel.class);

        OrderModel order = orderService.getModelOne(FrameUtil.newHashMap("order_no", notice.getAgent_order_id()), DbWREnums.READ);
        Assert.notNull(order, "订单不存在");

        // 更新申请状态为申请中
        FundLoanApplyModel apply = new FundLoanApplyModel();
        apply.setUser_id(order.getUser_id());
        apply.setOrder_id(order.getOrder_id());
        apply.setBus_type(BusTypeEnum.LOAN_APPLY.getCode());

        // 放款结果，1成功，2失败，3审核拒绝
        switch (notice.getRemit_result()){
            case 1:{
                fundLoanApplyService.loanApplyPassProcess(order.getOrder_id());
                apply.setStatus(LoanApplyStatusEnum.REPAYMENTS.getCode());
                break;
            }
            case 2:{
                //贷款通知 审核失败
                fundLoanApplyService.loanApplyFailProcess(order.getOrder_id(),notice.getMsg());
                apply.setStatus(LoanApplyStatusEnum.REFUSE.getCode());
                apply.setRefuse_msg(notice.getMsg());
                apply.setRefuse_code(notice.getCode()+"");
                break;

            }
            case 3:{
                //贷款通知审核拒绝
                fundLoanApplyService.loanApplyFailProcess(order.getOrder_id(),notice.getMsg());
                apply.setStatus(LoanApplyStatusEnum.REFUSE.getCode());
                apply.setRefuse_msg(notice.getMsg());
                apply.setRefuse_code(notice.getCode()+"");
                break;
            }default:{
                orderService.merchantRefuse(order.getOrg_id(), order.getOrder_id(), OrderRefuseEnum.C, notice.getMsg());
                apply.setStatus(LoanApplyStatusEnum.REFUSE.getCode());
            }
        }

        this.fundLoanApplyService.update(apply);
    }
}
